<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/6
 * Time: 下午5:29
 * Describe:
 */

namespace services\sms;


/**
 * Class ZenAoSms
 * 正奥
 */
class ZhengAoSms extends SmsBase
{
    public $userid;
    public $account;
    public $password;
    /**
     * @var string $urlCall 取得上行短信地址
     */
    protected $urlCall = 'http://www.591duanxin.com/callApi.aspx';

    /**
     * @var string 取得短信发送状态api url
     */
    private $urlStatus = 'http://www.591duanxin.com/statusApi.aspx';


    public function __construct()
    {
        parent::__construct();
        $this->url = 'http://www.591duanxin.com/sms.aspx';
    }

    /**
     * @param string $cell
     * @param string $content
     * @param string $sendTime
     * @return array
     */
    protected function getDataStruct($cell = '', $content = '', $sendTime = '')
    {
        return array(
            'userid' => $this->userid,
            'account' => $this->account,
            'password' => $this->password,
            'mobile' => $cell,
            'content' => $content,
            'sendtime' => $sendTime,

        );
    }


    /**
     * @param array|string $cell
     * @param string $content
     * @param int $uid
     * @param int $department
     * @param string $sendTime
     * @return bool|mixed
     *
     * $a = new ZhengAoSms;
     * $a->setSingleMsm('18888888888', 'message context');
     *
     */
    public function setSingleMsm($cell, $content, $uid = 0, $department = 12, $sendTime = '')
    {
        $post_data = $this->getDataStruct($cell, $content, $sendTime);
        if ($post_data['mobile'] == '' || $post_data['content'] == '') {
            return false;
        }
        $check_flag = $this->cellCheck($cell, $content);

        if ($check_flag == true) {
            $this->saveErrorMsg('setSingleMsm', $content, '频繁发送', $cell);
            return false;
        }

        $result = $this->curl($this->url . '?action=send', $post_data, 'post');
        $result = $this->parseResult($result);

        if (isset($result['returnstatus']) && strtolower($result['returnstatus']) == 'success' && $result['taskID']) {
            $taskId = $result['taskID'];
            $this->saveData($taskId, $cell, $content, $uid, $department, $sendTime);

        } else {
            $this->saveErrorMsg('setSingleMsm', $post_data, $result, $cell);
        }

        return $result;
    }


    /**
     * @return int
     * 取得上行短信并保存
     */
    public function getReceivedMsg()
    {
        $data = $this->getDataStruct();
        $data['action'] = 'query';
        $result = $this->curl($this->urlCall, $data);
        $resultArr = $this->parseResult($result);
        $messageTotal = 0;
        if (isset($resultArr['callbox'])) {
            $messageTotal = count($resultArr['callbox']);
        }
        foreach ($resultArr['callbox'] as $message) {
            $this->saveReceived(
                $message['taskid'],
                $message['mobile'],
                $message['content'],
                str_replace('/', '-', $message['receivetime'])
            );

        }

        return $messageTotal;
    }

    /**
     * @return bool|mixed
     */
    public function getUserInfo()
    {
        $url = $this->url . '?action=overage';

        $data = $this->getDataStruct();

        $result = $this->curl($url, $data);

        $resultArr = $this->parseResult($result);
        return (int)$resultArr['overage'];
    }

    /**
     * @param string $context 接口返回数据
     * @return bool|mixed
     */
    public function parseResult($context)
    {
        if (!$dom = simplexml_load_string($context)) {
            return false;
        }
        return json_decode(json_encode($dom), true);
    }

    public function getSmsStatus()
    {

        $data = $this->getDataStruct();
        $data['action'] = 'query';
        $result = $this->curl($this->urlStatus, $data);

        $resultArr = $this->parseResult($result);
        if (array_key_exists('errorstatus', $resultArr)) {
            $this->saveErrorMsg('取回状态', $data, $result);
            return false;
        }


        foreach ($resultArr['statusbox'] as $node) {
            $this->updateStatus($node['taskid'], $node['mobile'], $node['errorcode'], str_replace('/', '-', $node['receivetime']));
        }

        return true;
    }

}
